package handle

import (
	"gitee.com/crack007/goose"
	"gitee.com/crack007/goose/core/server"
	"gitee.com/crack007/udp-penetrate/src/common/protocol"
)

type udpServerSessionHandle struct {
	server.DefaultUdpSessionHandle
}

func NewUdpServerSessionHandle() *udpServerSessionHandle {
	return &udpServerSessionHandle{}
}

func (h *udpServerSessionHandle) OnHandshake() bool {
	return true
}

func (h *udpServerSessionHandle) OnMessage(data []byte) {
	iProtocol, err := protocol.UnpackData(data)
	defer func() {
		if err != nil {
			goose.GetLogger().Error(err)
			//h.Session.Close()
			return
		}
	}()
	if iProtocol == nil {
		return
	}
	resp := iProtocol.Process(h.Session, nil)
	if resp == nil {
		return
	}
	packData, err := protocol.PackData(resp)
	if err != nil {
		return
	}
	h.Session.Send(packData)
}
